import axios from 'axios';

// 创建一个 axios的应用实例 里面可以进行一些基础的配置
const $api = axios.create({
  baseURL: 'http://127.0.0.1:3030', // 公共前置链接

});


$api.interceptors.request.use((config) => {

  const actk = window.localStorage.getItem('accessToken');

  if (actk) config.headers.Authorization = `bearer ${actk}`;
  return config;
});


$api.interceptors.response.use(
  (response) => response,
  (err) => {
    let message = '';
    switch (err.response.status) {
      case 401:
        message = '未授权请重新登录';
        setTimeout(() => (window.location.href = '/#/login'), 1500);
        break;
      case 403:
        message = '服务器拒绝访问';
        break;
      default:
        message = `请求错误${err.request.status}`;
    }

  },
);

export default $api;
